layui.define(['layer','element', 'form','jquery','upload'],function(exports) {
    var layer = layui.layer //弹层
        ,element = layui.element //元素操作
        ,$ = layui.jquery
        ,form = layui.form
        ,upload = layui.upload;


    var userInfo = null;

    var userInfoPage = {
        //初始化员工表信息
        init_user_form_Info:function(){
            //拿到员工信息
            $.ajax({
                type: "GET",
                contentType: "application/x-www-form-urlencoded",//设置请求参数类型为json字符串
                url: "/manage/employee/info",
                success: function (resp) {
                    var code = resp.code;
                    var data = resp.data;
                    if (code === 200) {
                        $("#userId").val(data.userid);
                        $("#userName").val(data.userName);
                        $("#employeeName").val(data.employeeName);
                        $("#phone").val(data.phone);
                        $("#mailbox").val(data.mailbox);
                        $("#identityCard").val(data.identityCard);
                        $("#employeeAddress").val(data.employeeAddress);
                        $("#birthday").val(data.birthday);
                        $("#images").val(data.images);
                        if (data.images !== undefined && data.images !== "") {
                            $('#visit_images').attr('src', data.images);
                        }
                        var userId = data.userid;
                        let employeeId = data.employeeid;
                        userInfoPage.upload_user_image(userId);
                        userInfoPage.update_user_Info_from(employeeId);
                        //选完文件后不自动上传

                        form.render();
                    } else {
                        layer.msg("失败", {icon: 5});
                    }
                }
            });
        },

        //上传用户头像
        upload_user_image:function(Id){
            upload.render({
                elem: '#choose_images'
                ,url: '/system/user/uploadImage/'+ Id
                ,auto: false
                //,multiple: true
                ,bindAction: '#upload_images'
                , choose: function (obj) {
                    obj.preview(function(index, file, result){
                        $('#visit_images').attr('src', result).attr('width',100).attr('height',120); //图片链接（base64）
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 201){
                        return layer.msg('上传失败 '+ res.msg,{icon: 5});
                    }else {
                        //上传成功
                        $('#images').val(res.data);
                        return layer.msg('上传成功' , {icon: 1});
                    }
                }
            });

        },

        //更新用户信息
        update_user_Info_from:function(Id){
            form.on('submit(submitInfo)', function(){
                $.ajax({
                    type: "PUT",
                    contentType:"application/json",//设置请求参数类型为json字符串
                    url: "/manage/employee/" + Id,
                    data: JSON.stringify({
                        "phone": $("#phone").val(),
                        "mailbox":$("#mailbox").val(),
                        "identityCard":$("#identityCard").val(),
                        "employeeAddress":$("#employeeAddress").val(),
                        "birthday":$("#birthday").val(),
                    }),
                    success:function(resp){
                        var code = resp.code;
                        if (code === 200 ){
                            layer.msg('保存成功', {icon: 1});
                        }else {
                            layui.form.render();
                            let errorMsg;
                            resp.msg==undefined?errorMsg="保存失败": errorMsg="保存失败,"+resp.msg;
                            layer.msg( errorMsg, {icon: 5});
                        }
                    }
                });


            });



        }

    };


    userInfoPage.init_user_form_Info();


    exports("userInfoPage",userInfoPage);




});